﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLG.Domain.Services
{
    public class QueryService
    {
        public SingleResult<Datas.KingdomAndKing> QueryKingdom(int userId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_Kingdom> kdRep = new EntRep<Entities.T_Kingdom>(dbContext);
                EntRep<Entities.T_King> kRep = new EntRep<Entities.T_King>(dbContext);

                Entities.T_Kingdom kingdom = kdRep.Get(p => p.UserId == userId && p.ServerId == Config.ServerId);
                Entities.T_King king = kRep.Get(p => p.UserId == userId && p.ServerId == Config.ServerId);
                if(kingdom != null && king !=null)
                {
                    return new SingleResult<Datas.KingdomAndKing>(new Datas.KingdomAndKing(kingdom, king));
                }
                return new SingleResult<Datas.KingdomAndKing>(Library.ErrorCodes.Com_ObjectNotExist);
            }
        }

        public SingleResult<Datas.CityAndResouce> QueryCityAndResource(int userId, Guid cityId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_City> cRep = new EntRep<Entities.T_City>(dbContext);
                EntRep<Entities.T_Resource> rRep = new EntRep<Entities.T_Resource>(dbContext);

                Datas.CityAndResouce city = new Datas.CityAndResouce();
                city.City = cRep.Get(p => p.UserId == userId && p.Id == cityId);
                

                city.Resources = rRep.GetList(p => p.CityId == city.City.Id && p.UserId == userId);
                // 计算资源清算时间
                DateTime now = DateTime.Now;
                if ((now - city.City.ResLiquidatedTime).Ticks > TimeSpan.FromMinutes(1).Ticks)
                {
                    city.City.ResLiquidatedTime = now;
                    cRep.Update(city.City);

                    // 更新资源
                    for (int index = 0; index < city.Resources.Count; index++)
                    {
                        city.Resources[index].Value += city.Resources[index].Rate;
                        city.Resources[index].LiquidatedTime = now;
                        rRep.Update(city.Resources[index]);
                    }

                    try
                    {
                        dbContext.SaveChanges();
                    }
                    catch(Exception ex)
                    {
                        
                    }
                }

                return new SingleResult<Datas.CityAndResouce>(city);
            }
        }

        public ListResult<Entities.T_City> QueryCities(int userId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_City> cRep = new EntRep<Entities.T_City>(dbContext);
                List<Entities.T_City> list = cRep.Query(p => p.UserId == userId && p.ServerId == Config.ServerId).OrderBy(p => p.Level).ThenBy(p => p.Name).ToList();
                return new ListResult<Entities.T_City>(list);
            }
        }

        public ListResult<Entities.T_Hero> QueryHeros(int userId, Guid cityId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_Hero> rep = new EntRep<Entities.T_Hero>(dbContext);
                List<Entities.T_Hero> list = rep.Query(p => p.UserId == userId && p.ServerId == Config.ServerId && p.CityId == cityId).OrderBy(p => p.Level).ThenBy(p => p.Name).ToList();
                return new ListResult<Entities.T_Hero>(list);
            }
        }

        public ListResult<Entities.T_Building> QueryBuildings(int userId, Guid cityId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_Building> rep = new EntRep<Entities.T_Building>(dbContext);
                List<Entities.T_Building> list = rep.Query(p => p.UserId == userId
                    && p.ServerId == serId
                    && p.CityId == cityId).OrderBy(p => p.Type).ThenBy(p => p.Level).ToList();
                return new ListResult<Entities.T_Building>(list);
            }
        }

        public ListResult<Entities.T_Army> QueryArmies(int userId, Guid cityId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_Army> aRep = new EntRep<Entities.T_Army>(dbContext);
                List<Entities.T_Army> list = aRep.Query(p => p.UserId == userId && p.CityId == cityId).ToList();
                return new ListResult<Entities.T_Army>(list);
            }
        }

        public SingleResult<Datas.ArmyDetail> QueryArmy(int userId, Guid armyId)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Entities.T_Army> aRep = new EntRep<Entities.T_Army>(dbContext);
                EntRep<Entities.T_Army_Unit> auRep = new EntRep<Entities.T_Army_Unit>(dbContext);
                Entities.T_Army army = aRep.Get(p => p.UserId == userId && p.Id == armyId);
                List<Entities.T_Army_Unit> units = auRep.GetList(p => p.ArmyId == armyId);

                Datas.ArmyDetail detail = new Datas.ArmyDetail();
                detail.Army = army;
                detail.Units = units;
                return new SingleResult<Datas.ArmyDetail>(detail);
            }
        }
    }
}
